package com.example.friendcash.activities;

import android.content.Context;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AbsMenuActivity extends FragmentActivity {

    private PopupMenu mPopupMenu;

    private int mMenuRes;
    private Menu mActualMenu;

    private boolean canPopup() {
        return Build.VERSION.SDK_INT >= 11;
    }

    public void setupOverflowMenu(int menuRes, View button) {
        mMenuRes = menuRes;
        button.setVisibility(View.VISIBLE);
        if (canPopup()) {
            mPopupMenu = new PopupMenu(this, button);
            try {
                Field[] fields = mPopupMenu.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if ("mPopup".equals(field.getName())) {
                        field.setAccessible(true);
                        Object menuPopupHelper = field.get(mPopupMenu);
                        Class<?> classPopupHelper = Class.forName(
                                menuPopupHelper.getClass().getName());
                        Method setForceIcons = classPopupHelper.getMethod(
                                "setForceShowIcon", boolean.class);
                        setForceIcons.invoke(menuPopupHelper, true);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mPopupMenu.getMenuInflater().inflate(menuRes, mPopupMenu.getMenu());
            mActualMenu = mPopupMenu.getMenu();
            mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    return onMenuItemSelected(0, item);
                }
            });
        } else {
            mActualMenu = newMenuInstance();
            getMenuInflater().inflate(menuRes, mActualMenu);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (canPopup()) mPopupMenu.show();
                else openOptionsMenu();
            }
        });
    }

    private Menu newMenuInstance() {
        try {
            Class<?> menuBuilderClass = Class.forName(
                    "com.android.internal.view.menu.MenuBuilder");
            Constructor<?> constructor = menuBuilderClass
                    .getDeclaredConstructor(Context.class);
            return (Menu) constructor.newInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU &&
                canPopup() && mPopupMenu != null) {
            mPopupMenu.show();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (canPopup() || mMenuRes == 0) {
            return super.onCreateOptionsMenu(menu);
        } else {
            getMenuInflater().inflate(mMenuRes, menu);
            mActualMenu = menu;
            return true;
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            MenuItem actual = mActualMenu.getItem(i);
            item.setTitle(actual.getTitle());
            item.setVisible(actual.isVisible());
        }
        return true;
    }

}
